package edu.fsu.cs.cis5930;

import java.util.LinkedList;
import java.util.List;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.preference.PreferenceManager;


public class ContentProviderBackup extends SQLiteOpenHelper {

	private SharedPreferences myPrefs;
	private SharedPreferences.Editor editor;
	 private static final int DATABASE_VERSION = 2;
	private static final String CARD_TABLE_NAME = "cards";
	 private static final String CARD_TABLE_CREATE =
         "CREATE TABLE " + CARD_TABLE_NAME + " (" +
         "id" + " INTEGER PRIMARY KEY, " +
         "first_name" + " TEXT, " +
         "last_name" + " TEXT, " +
         "phone_number" + " TEXT, " +
         "email" + " TEXT);";
	
	
	public ContentProviderBackup(Context context) {
		 super(context, CARD_TABLE_NAME, null, DATABASE_VERSION);
		myPrefs = PreferenceManager.getDefaultSharedPreferences(context);
		editor = myPrefs.edit();
	}
	
	void setMyCard(BusinessCard c)
	{
		editor.putString("first_name", c.first_name);
		editor.putString("last_name", c.last_name);
		editor.putString("phone_number", c.phone_number);
		editor.putString("email", c.email);
		editor.commit();
	}
	
	BusinessCard getMyCard()
	{
		BusinessCard myCard = new BusinessCard();
		String defaultValue = "unspecified";
		myCard.first_name = myPrefs.getString("first_name", defaultValue);
		myCard.last_name = myPrefs.getString("last_name", defaultValue);
		myCard.phone_number = myPrefs.getString("phone_number", defaultValue);
		myCard.email = myPrefs.getString("email", defaultValue);
		return myCard;		
	}

	
	List<BusinessCard> getAllCards()
	{
		List<BusinessCard> myList = new LinkedList<BusinessCard>();
		SQLiteDatabase myDB = getReadableDatabase();
		
		Cursor cur = myDB.query(CARD_TABLE_NAME, new String[] {"id", "first_name", "last_name", "phone_number", "email"}, 
               null , null, null, null, null);
		 cur.moveToFirst();
	        while (cur.isAfterLast() == false) {
	        	BusinessCard newCard = new BusinessCard();
	        	newCard.ID = cur.getInt(0);
	        	newCard.first_name = cur.getString(1);
	        	newCard.last_name = cur.getString(2);
	        	newCard.phone_number = cur.getString(3);
	        	newCard.email = cur.getString(4);
	        	myList.add(newCard);
	          
	       	    cur.moveToNext();
	        }
	        cur.close();
	        myDB.close();
	        
	        return myList;
		
	}
	
	void deleteCard( int id)
	{
		SQLiteDatabase myDB = getWritableDatabase();
		
	     myDB.execSQL("DELETE FROM " + CARD_TABLE_NAME + " WHERE id=" + id + ";");
	     myDB.close();
	}
	
	void insertCard( BusinessCard c)
	{
		SQLiteDatabase myDB = getWritableDatabase();
		ContentValues myValue = new ContentValues();
		myValue.put("first_name", c.first_name);
		myValue.put("last_name", c.last_name);
		myValue.put("phone_number", c.phone_number);
		myValue.put("email", c.email);
		myDB.insert(CARD_TABLE_NAME, null, myValue);
		myDB.close();
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		 db.execSQL(CARD_TABLE_CREATE);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
	    db.execSQL("DROP TABLE IF EXISTS " + CARD_TABLE_NAME);
	    onCreate(db);
		
	}
	

}
